package com.example.demo.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>DESC: 模板模式  </p>
 * <p>2018/4/10</p>
 * <p>AUTHOR:DengC</p>
 */
@NoRepositoryBean//禁止实例化
public interface BaseDAO<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {

    /**
     * 通过id获取数据
     *
     * @param id id
     * @return T
     */
    @Query(value = "select t from #{#entityName} t where t.id= ?1")
    T getById(String id);

    /**
     * 获取所有
     *
     * @return
     */
    @Query(value = "select t from #{#entityName} t ")
    List<T> listAll();

    /**
     * 通过id删除
     *
     * @param id id
     */
    @Modifying
    @Transactional
    @Query(value = "delete from  #{#entityName} t where t.id=?1")
    void delById(String id);


}
